package chan.project.smartlawdatamanage.job;

import chan.project.smartlawdatamanage.mapper.LawRegulationInvokeLogMapper;
import chan.project.smartlawdatamanage.service.LawRegulationInvokeLogService;
import chan.project.smartlawdatamanage.service.TemLawRegulationInvokeLogService;
import chan.project.smartlawmodel.entity.LawRegulationInvokeLog;
import chan.project.smartlawmodel.entity.TemLawRegulationInvokeLog;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import jakarta.annotation.Resource;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.List;

@Component
public class TemLawRegulationSave {

    @Resource
    private TemLawRegulationInvokeLogService temLawRegulationInvokeLogService;

    @Resource
    private LawRegulationInvokeLogMapper lawRegulationInvokeLogMapper;

    @Scheduled(cron = "0 0 2 * * ?")
    public void save() {
        //临时数据转化
        List<LawRegulationInvokeLog> hadLaw = lawRegulationInvokeLogMapper.getHadLaw();
        if(!CollectionUtils.isEmpty(hadLaw)){
            for (LawRegulationInvokeLog lawRegulationInvokeLog : hadLaw) {
                lawRegulationInvokeLogMapper.insert(lawRegulationInvokeLog);
            }
        }
    }

    /**
     * 定期清理长期数据
     */
    @Scheduled(cron = "0 0 3 * * ?")
    public void clear(){
        LocalDateTime tenDaysAgo = LocalDateTime.now().minusDays(10);
            LambdaQueryWrapper<TemLawRegulationInvokeLog> queryWrapper =
                new LambdaQueryWrapper<>();
        queryWrapper.lt(TemLawRegulationInvokeLog::getSendTime, tenDaysAgo);
        temLawRegulationInvokeLogService.remove(queryWrapper);
    }
}
